package com.eqdd.yiqidian.ui.yiqixue.info;

import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.widget.TextView;

import com.eqdd.yiqidian.LectureInfoActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.GridSpacingItemDecoration;
import com.eqdd.yiqidian.adapter.recycleadapter.ItemClickSupport;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.HttpResultRows;
import com.eqdd.yiqidian.bean.rebuild.LearnVideoListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnPhotoListBean;
import com.eqdd.yiqidian.bean.rebuild.LearnVideoListBean;
import com.eqdd.yiqidian.box.span.MovementMethod.ClickMovementMethod;
import com.eqdd.yiqidian.box.span.TagSpan;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.YoukuPlayActivity;
import com.eqdd.yiqidian.ui.yiqixue.LearnPhotoActivity;
import com.eqdd.yiqidian.ui.yiqixue.LearnSearchActivity;
import com.eqdd.yiqidian.utils.DateUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.ScrollLoadMoreUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 18-3-8  下午4:23.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class LectureInfoActivity extends BaseActivity {

    private LectureInfoActivityCustom dataBinding;
    private int id;
    private SlimAdapterEx<LearnVideoListBean> slimAdapterExVideo;
    private boolean isMore;
    private int nextPage;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_lecture_info);
        initTopTitleBar(View.VISIBLE, "课程详情");
    }

    @Override
    public void initData() {
        id = getIntent().getIntExtra(Config.ID, 0);

    }

    @Override
    public void setView() {
        OkGo.<HttpResult<LectureInfo>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_LECTURE_INFO)
                .params("courseId", id)
                .execute(new DialogCallBack<HttpResult<LectureInfo>>(LectureInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<LectureInfo>> response) {
                        HttpResult<LectureInfo> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {

                            updateView(httpResult.getItems());
                        }
                    }
                });
    }

    private void updateView(LectureInfo items) {
        dataBinding.tvTitle.setText(items.getCourseTheme());
        dataBinding.tvTags.setText("");
        String[] split = items.getCourseType().split(",");
        for (String s : split) {
            SpannableString spanString = new SpannableString(s);
            TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);

            spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
            dataBinding.tvTags.append(spanString);
        }
        dataBinding.tvAuthor.setText(items.getLectureName());
        dataBinding.tvTime.setText(items.getCreateTime());
        dataBinding.tvObject.setText("授课对象:" + items.getCourseStringer());
        dataBinding.tvTarget.setText("课程目标:" + items.getCourseTarget());
        dataBinding.tvBg.setText("课程背景:" + items.getCourseBackground());
        dataBinding.tvPrice.setText("参考价格:" + items.getCoursePrice() + "/天");
        dataBinding.tvLectureTime.setText("课程时长:" + items.getCourseTimes());
        dataBinding.tvContent.setText(Html.fromHtml(items.getCourseOutlint()));
        dataBinding.recyclerViewPic.setLayoutManager(new GridLayoutManager(this, 3));
        dataBinding.recyclerViewPic.addItemDecoration(new GridSpacingItemDecoration(3, 15, true));
        SlimAdapterEx<String> slimAdapterEx = SlimAdapterEx.create().register(R.layout.grid_item_photo, new SlimInjector<String>() {
            @Override
            public void onInject(String data, IViewInjector injector) {
                injector.image(R.id.grid_img, data);
            }
        }).attachTo(dataBinding.recyclerViewPic).updateData(items.getCourseImage());
        ItemClickSupport.addTo(dataBinding.recyclerViewPic).setOnItemClickListener((recyclerView, position, v) -> {
            ArrayList<LocalMedia> localMedia = new ArrayList<>();
            for (int i = 0; i < slimAdapterEx.getData().size(); i++) {
                localMedia.add(PicUtil.localMedia(slimAdapterEx.getDataItem(i)));
            }
            PicUtil.preview(this, position, localMedia);
        });

        
        dataBinding.recyclerViewVideo.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterExVideo = SlimAdapterEx.create().register(R.layout.list_item_112, new SlimInjector<LearnVideoListBean>() {
            @Override
            public void onInject(LearnVideoListBean data, IViewInjector injector) {
                injector.image(R.id.iv_poster, data.getVideoImage())
                        .text(R.id.tv_title, data.getVideoTitle())
                        .text(R.id.tv_time, data.getVideoTime())
                        .with(R.id.tv_types, view -> {
                            TextView textView = (TextView) view;
                            textView.setText("");
                            String[] split = data.getLabel().split(",");
                            for (String s : split) {
                                SpannableString spanString = new SpannableString(s);
                                TagSpan tagSpan = new TagSpan(Color.GREEN, 10, 10);
                                tagSpan.setTextClick(text -> {
                                    Bundle bundle = new Bundle();
                                    bundle.putInt(Config.POS, 2);
                                    bundle.putString(Config.FIELD, text.toString());
                                    startActivity(LearnSearchActivity.class, bundle);
                                });
                                spanString.setSpan(tagSpan, 0, s.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
                                textView.append(spanString);
                            }
                            textView.setMovementMethod(ClickMovementMethod.getInstance());
                        });
            }
        }).attachTo(dataBinding.recyclerViewVideo).updateData(new ArrayList());
        ItemClickSupport.addTo(dataBinding.recyclerViewVideo)
                .setOnItemClickListener((recyclerView, position, v) -> {
                    Bundle bundle = new Bundle();
                    bundle.putString(Config.URL, slimAdapterExVideo.getDataItem(position).getVideoUrl());
                    bundle.putString(Config.CONTENT, slimAdapterExVideo.getDataItem(position).getDescribe());
                    startActivity(YoukuPlayActivity.class, bundle);
                });
        ScrollLoadMoreUtil.loadMore(dataBinding.nestedScrollView, () -> {
            if (isMore) {
                loadVideoData(nextPage);

            }
        });
        loadVideoData(0);
    }

    private void loadVideoData(int page) {
        OkGo.<HttpResultRows<LearnVideoListBean>>post(HttpConfig.BASE_URL + HttpConfig.LEARN_LECTURE_VIDEO)
                .params("page", page)
                .params("courseId", id)
                .execute(new DialogCallBack<HttpResultRows<LearnVideoListBean>>(LectureInfoActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResultRows<LearnVideoListBean>> response) {
                        HttpResultRows<LearnVideoListBean> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            if (page == 0) {
                                slimAdapterExVideo.updateData(httpResult.getItems().getRows());
                            } else {
                                int startPos = slimAdapterExVideo.getData().size();
                                slimAdapterExVideo.getData().addAll(httpResult.getItems().getRows());
                                slimAdapterExVideo.notifyItemRangeInserted(startPos, httpResult.getItems().getRows().size());
                            }
                            if (httpResult.getItems().getRows().size() == 0) {

                                dataBinding.tvStatus.setText("没有数据了");
                                isMore = false;
                            } else {
                                dataBinding.tvStatus.setText("加载数据中...");
                                nextPage = httpResult.getItems().getPage();
                                isMore = true;
                            }
                        }
                    }
                });
    }

    public class LectureInfo {

        /**
         * courseImage : ["https://www.eqid.top:8009/LectureImage/20180109/2018010917064400831.jpg","https://www.eqid.top:8009/LectureImage/20180109/2018010917064411761.jpg","https://www.eqid.top:8009/LectureImage/20180109/2018010917064432239.jpg","https://www.eqid.top:8009/LectureImage/20180109/2018010917064442410.png","https://www.eqid.top:8009/LectureImage/20180109/2018010917064452644.png","https://www.eqid.top:8009/LectureImage/20180109/2018010917064462934.jpg","https://www.eqid.top:8009/LectureImage/20180109/2018010917064473244.png","https://www.eqid.top:8009/LectureImage/20180109/2018010917064483494.png","https://www.eqid.top:8009/LectureImage/20180109/2018010917064494313.png"]
         * Id : 6
         * courseType : 类型
         * courseTheme : 主题
         * courseTimes : 课程时长
         * courseOutlint : 课程大纲
         * courseStringer : 授课对象
         * userGuid : 1221b0fed5ec43d88e6b0d5a08e50e79
         * createTime : 2018-01-09T17:06:44
         * lectureName : 讲师名字
         * updater : null
         * updateTime : null
         * isdel : null
         * status : 0
         * courseMethod : 授课方法
         * coursePrice : 10000
         * courseBackground : 课程背景
         * courseTarget : 课程目标
         * courseImages : /LectureImage/20180109/2018010917064400831.jpg;/LectureImage/20180109/2018010917064411761.jpg;/LectureImage/20180109/2018010917064432239.jpg;/LectureImage/20180109/2018010917064442410.png;/LectureImage/20180109/2018010917064452644.png;/LectureImage/20180109/2018010917064462934.jpg;/LectureImage/20180109/2018010917064473244.png;/LectureImage/20180109/2018010917064483494.png;/LectureImage/20180109/2018010917064494313.png
         * commentCount : 0
         * coursewares : https://www.eqid.top:8009/LectureCourse/20180109/2018010917064400430.png
         * courseWareName : daminghu.png
         */

        private int Id;
        private String courseType;
        private String courseTheme;
        private String courseTimes;
        private String courseOutlint;
        private String courseStringer;
        private String userGuid;
        private String createTime;
        private String lectureName;
        private String updater;
        private String updateTime;
        private String isdel;
        private int status;
        private String courseMethod;
        private int coursePrice;
        private String courseBackground;
        private String courseTarget;
        private String courseImages;
        private int commentCount;
        private String coursewares;
        private String courseWareName;
        private List<String> courseImage;

        public int getId() {
            return Id;
        }

        public void setId(int Id) {
            this.Id = Id;
        }

        public String getCourseType() {
            return courseType;
        }

        public void setCourseType(String courseType) {
            this.courseType = courseType;
        }

        public String getCourseTheme() {
            return courseTheme;
        }

        public void setCourseTheme(String courseTheme) {
            this.courseTheme = courseTheme;
        }

        public String getCourseTimes() {
            return courseTimes;
        }

        public void setCourseTimes(String courseTimes) {
            this.courseTimes = courseTimes;
        }

        public String getCourseOutlint() {
            return courseOutlint;
        }

        public void setCourseOutlint(String courseOutlint) {
            this.courseOutlint = courseOutlint;
        }

        public String getCourseStringer() {
            return courseStringer;
        }

        public void setCourseStringer(String courseStringer) {
            this.courseStringer = courseStringer;
        }

        public String getUserGuid() {
            return userGuid;
        }

        public void setUserGuid(String userGuid) {
            this.userGuid = userGuid;
        }

        public String getCreateTime() {
            return DateUtil.format(createTime);
        }

        public void setCreateTime(String createTime) {
            this.createTime = createTime;
        }

        public String getLectureName() {
            return lectureName;
        }

        public void setLectureName(String lectureName) {
            this.lectureName = lectureName;
        }

        public String getUpdater() {
            return updater;
        }

        public void setUpdater(String updater) {
            this.updater = updater;
        }

        public String getUpdateTime() {
            return updateTime;
        }

        public void setUpdateTime(String updateTime) {
            this.updateTime = updateTime;
        }

        public String getIsdel() {
            return isdel;
        }

        public void setIsdel(String isdel) {
            this.isdel = isdel;
        }

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }

        public String getCourseMethod() {
            return courseMethod;
        }

        public void setCourseMethod(String courseMethod) {
            this.courseMethod = courseMethod;
        }

        public int getCoursePrice() {
            return coursePrice;
        }

        public void setCoursePrice(int coursePrice) {
            this.coursePrice = coursePrice;
        }

        public String getCourseBackground() {
            return courseBackground;
        }

        public void setCourseBackground(String courseBackground) {
            this.courseBackground = courseBackground;
        }

        public String getCourseTarget() {
            return courseTarget;
        }

        public void setCourseTarget(String courseTarget) {
            this.courseTarget = courseTarget;
        }

        public String getCourseImages() {
            return courseImages;
        }

        public void setCourseImages(String courseImages) {
            this.courseImages = courseImages;
        }

        public int getCommentCount() {
            return commentCount;
        }

        public void setCommentCount(int commentCount) {
            this.commentCount = commentCount;
        }

        public String getCoursewares() {
            return coursewares;
        }

        public void setCoursewares(String coursewares) {
            this.coursewares = coursewares;
        }

        public String getCourseWareName() {
            return courseWareName;
        }

        public void setCourseWareName(String courseWareName) {
            this.courseWareName = courseWareName;
        }

        public List<String> getCourseImage() {
            return courseImage;
        }

        public void setCourseImage(List<String> courseImage) {
            this.courseImage = courseImage;
        }
    }
}
